import redis  # 导入redis 模块
import requests
import time
import json


def get_ip():
    url = 'https://api.xiaoxiangdaili.com/ip/get?appKey=668366184475152384&appSecret=fYmjkOVM&cnt=&wt=json'
    rep = requests.get(url).json()['data'][0]
    print(rep)
    ip = rep['ip']
    port = rep['port']
    proxies = {
        'http': f'{ip}:{port}',
        'https': f'{ip}:{port}'
    }
    return proxies


def loop():
    while True:
        time.sleep(5)
        r = redis.Redis(host='localhost', port=6379, decode_responses=True)

        try:
            r.hset('xiaoxiang', 'ip', json.dumps(get_ip()))
        except:
            continue


if __name__ == '__main__':
    loop()
